<?php
class ProductcategoryController extends Controller
{
    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_productcategory")->queryScalar();
        $result = $this->connection->createCommand("select * from crm_productcategory limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionGetcomboboxdata(){
        $data = $this->connection->createCommand("select productcategory_id as value,productcategoryname label from crm_productcategory ")->queryAll();
        echo json_encode($data);
    }

    public function actionShowedit(){

        $productcategory_id = $_REQUEST['productcategory_id'];

        $productcategory = null;
        if($productcategory_id==0){
            //新增
        }else{
            //编辑
            $productcategory = $this->connection->createCommand("select * from crm_productcategory where productcategory_id = :productcategory_id")->bindParam(':productcategory_id',$productcategory_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['productcategory'=>$productcategory]);
    }

    public function actionSavedata(){

        $productcategory_id = $_REQUEST['productcategory_id'];
        $productcategoryname = $_REQUEST['productcategoryname'];

        if($productcategory_id!=null){
            //编辑
            $this->connection->createCommand("update crm_productcategory set productcategoryname = :productcategoryname  where productcategory_id = :productcategory_id")
                ->bindParam(':productcategory_id',$productcategory_id,PDO::PARAM_INT)
                ->bindParam(':productcategoryname',$productcategoryname,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_productcategory (productcategoryname) values (:productcategoryname) ")
                ->bindParam(':productcategoryname',$productcategoryname,PDO::PARAM_STR)
                ->execute();
        }
    }


}